﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "UI/WidgetController/OverlayWidgetController.h"

#include "AbilitySystem/StarrySkyAbilitySystemComponent.h"
#include "AbilitySystem/StarrySkyAttributeSet.h"

void UOverlayWidgetController::BroadcastInitialValues()
{
	//广播基础的血量魔力等属性
	OnHealthChanged.Broadcast(GetStarrySkyAS()->GetHealth());
	OnMaxHealthChanged.Broadcast(GetStarrySkyAS()->GetMaxHealth());
	OnManaChanged.Broadcast(GetStarrySkyAS()->GetMana());
	OnMaxManaChanged.Broadcast(GetStarrySkyAS()->GetMaxMana());
	//绑定游戏系统组件广播的所有游戏标签
	GetStarrySkyASC()->EffectAssetTags.AddLambda(
	[this](const FGameplayTagContainer& AssetTags)	
		{
			for (const FGameplayTag& Tag : AssetTags)
			{
				FGameplayTag MessageTag=FGameplayTag::RequestGameplayTag("Message");
				/**
				 *"A.1".MatchesTag("A") will return True, "A".MatchesTag("A.1") will return False
				 * Message.healthPoint.MatchesTag("Message") 这个返回true，以为Message.healthPoint是Message的子标签
				 */
				if(Tag.MatchesTag(MessageTag))
				{
					//这里通过MessageWidgetDataTable找到对应Tag的行数据
					const FUIWidgetRow* Row=GetDataTableRowByTag<FUIWidgetRow>(MessageWidgetDataTable,Tag);
					MessageWidgetRowDelegate.Broadcast(*Row);
				}
			}
		}
	);
}

void UOverlayWidgetController::BindCallbacksToDependencies()
{
	//绑定属性变化的动态多播代理
	//生命值的属性监听绑定
	AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(
	GetStarrySkyAS()->GetHealthAttribute()
	).AddLambda([this](const FOnAttributeChangeData& Data)
	{
		OnHealthChanged.Broadcast(Data.NewValue);
	});
	//最大生命值的属性监听绑定
	AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(
	GetStarrySkyAS()->GetMaxHealthAttribute()
	).AddLambda([this](const FOnAttributeChangeData& Data)
	{
		OnMaxHealthChanged.Broadcast(Data.NewValue);
	});
	//法术值的属性监听绑定
	AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(
	GetStarrySkyAS()->GetManaAttribute()).AddLambda([this](const FOnAttributeChangeData& Data)
	{
		OnManaChanged.Broadcast(Data.NewValue);
	});
	//最大法术值的属性监听绑定
	AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(
	GetStarrySkyAS()->GetMaxManaAttribute()).AddLambda([this](const FOnAttributeChangeData& Data)
	{
		OnMaxManaChanged.Broadcast(Data.NewValue);
	});
}

